package com.wx.service.impl;

import com.wx.mapper.OrderMapperCustom;
import com.wx.mapper.WxOrderMapper;
import com.wx.pojo.WxOrder;
import com.wx.pojo.WxUserProductDetail;
import com.wx.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.wx.utils.commonUtil.generateOrderNo;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private WxOrderMapper wxOrderMapper;
    @Autowired
    private OrderMapperCustom orderMapperCustom;
    @Autowired
    private IOrderService iOrderService;

    @Override
    public Integer insetModel(Integer user_id,String number,Integer amount,BigDecimal total_price) {

        WxOrder OrderModel= new WxOrder();
        OrderModel.setNumber(generateOrderNo("20"));
        OrderModel.setUserId(user_id);
        OrderModel.setCollectNumber(number);
        OrderModel.setAmount(amount);
        OrderModel.setTotalPrice(total_price);
        OrderModel.setStatus(1);
        OrderModel.setGmtCreate(new Date());
        OrderModel.setGmtModified(new Date());

        return wxOrderMapper.insert(OrderModel);
    }

    @Override
    public WxOrder getModel(String number,Integer user_id) {
        return orderMapperCustom.getModel(number,user_id);
    }

    @Override
    public void addOrderID(Integer user_id, String number, Integer amount, BigDecimal total_price) {

        //判断是否已经生成订单
        WxOrder orderModel= iOrderService.getModel(number,user_id);

        if (orderModel==null){
            //生成新订单
            iOrderService.insetModel(user_id,number,amount,total_price);
        }else{
            //修改订单 吨数和总金额
            WxOrder model= new WxOrder();
            model.setId(orderModel.getId());
            model.setNumber(orderModel.getNumber());
            model.setUserId(orderModel.getUserId());
            model.setCollectNumber(orderModel.getCollectNumber());
            model.setAmount(amount);
            model.setTotalPrice(total_price);
            model.setStatus(1);
            model.setGmtCreate(orderModel.getGmtCreate());
            model.setGmtModified(new Date());

            wxOrderMapper.updateByPrimaryKey(model);
        }
    }

    @Override
    public List<String> listDistinctModelByUserAndStatus(Integer user_id,Integer status){

        List<WxOrder> list= orderMapperCustom.listDistinctModelByUserAndStatus(user_id,status);
        List<String> list1=new ArrayList<>();
        for (WxOrder str:list) {
            list1.add(str.getCollectNumber());
        }
        return list1;
    }
}
